<?php
/**
 * A template based email system
 * @author Can.Nguyen
 */
class CMS_Mail
{
    
    /**
     * Variable registry for template values
     */
    protected $templateVariables = array ();
    
    /**
     * Template name
     */
    protected $templateName;
    
    /**
     * Zend_Mail instance
     */
    protected $zendMail;
    
    /**
     * Email recipient
     */
    protected $recipient;

    /**
     * __construct
     *
     * Set default options
     *
     */
    public function __construct()
    {
        $this->zendMail = new Zend_Mail ( );
    }

    /**
     * Set variables for use in the templates
     *
     * Magic function stores the value put in any variable in this class for
     * use later when creating the template
     *
     * @param string $name  The name of the variable to be stored
     * @param mixed  $value The value of the variable
     */
    public function __set($name, $value)
    {
        $this->templateVariables [$name] = $value;
    }

    /**
     * Set the template file to use
     *
     * @param string $filename Template filename
     */
    public function setTemplate($filename)
    {
        $this->templateName = $filename;
    }

    /**
     * Set the recipient address for the email message
     * 
     * @param string $email Email address
     */
    public function setRecipient($email)
    {
        $this->recipient [] = $email;
    }

    /**
     * Send the constructed email
     *
     * @todo Add from name
     */
    public function send()
    {
        /*
         * Get data from config
         */
        $config = Zend_Registry::get ( 'mail_config' );
        $templateDir = $config ['templateDir'];
        $fromAddr = $config ['sender'] ['from'];
        $fromName = $config ['sender'] ['name'];
        // get view 
        $viewConfig = array ('basePath' => $templateDir, 'scriptPath' => $templateDir );
        $viewMail = new Zend_View ( $viewConfig );
        foreach ( $this->templateVariables as $key => $value )
        {
            $viewMail->{$key} = $value;
        }
        
        /*
         * Get data for mail
         */
        $subject = $viewMail->render ( $this->templateName . '.subj' );
        $contentText = $viewMail->render ( $this->templateName . '.phtml' );
        
        $viewMail->content = $viewMail->render ( $this->templateName . '-html.phtml' );
        $contentHtml = $viewMail->render ( 'template-mail-html.phtml' );
        
        // Send mail
        $mail = new Zend_Mail('UTF-8');
        $mail->setFrom ( $fromAddr, $fromName );
        foreach($this->recipient as $to )
        {
        	$mail->addTo ( $to );
        }
        
        $mail->setSubject ( $subject );
        $mail->setBodyText ( $contentText );
        $mail->setBodyHtml ( $contentHtml );
        
        try
        {
            $mail->send ();
        }
        catch ( Exception $ex )
        {
        	print_R($ex->__toString());exit;
        }
    }

}
?>